// pages/Review_words/Review_words.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 单词
    content: null,
    // 音标
    pron: null,
    // 词性
    definition: null,
    Id: 0,
    // 读音src
    audioURL: null,
    // 单词的翻译是否显示
    showNot_definition: false,
    // 单词是否显示
    showNot_word: false,
    // 复习的单词词库
    review_word: [],
    // 当前复习的单词列表
    List: [],
    // 计数器
    count: 0,
    // 任务目标单词数
    goal: 0,
    // 是否一键显示翻译(采用得是hidden)
    show_definition: true,
    // 是否一键显示单词（采用wx：if）
    show_word: false,
    show1: true,
    show2: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 选择的词库
    var list2 = require('../../data/cet4.js')
    var list3 = require('../../data/cet6.js')
    var list4 = require('../../data/kaoyan.js')
    var list5 = require('../../data/cet4_import.js')
    var list6 = require('../../data/cet6_import.js')
    var list7 = require('../../data/kaoyan_import.js')
    //  获取选择的词库
    var checked = wx.getStorageSync("checked")
    console.log(checked);
    switch (checked) {
      case 'cet4':
        this.setData({
          list: list2,
        });
        break;
      case 'cet6':
        this.setData({
          list: list3,
        });
        break;
      case '考研':
        this.setData({
          list: list4,
        });
        break;
      case 'cet4高频':
        this.setData({
          list: list5,
        });
        break;
      case 'cet6高频':
        this.setData({
          list: list6,
        });
        break;
      case '考研高频':
        this.setData({
          list: list7,
        });
        break;
    }

    // 获取缓存中的id值
    var id = wx.getStorageSync("previous")
    console.log("id值", id)
    var goal = wx.getStorageSync("goal")
    console.log("goal值", goal)
    var review_word
    if(goal>=id){
      goal=id;
    }
    // review_word: this.data.list.wordList.slice(0, id)
    this.setData({
      review_word: this.data.list.wordList.slice(0, id),
      Id: id,
      goal: goal,
    })
    console.log("复习的词库", this.data.review_word);

    // 从已学的单词中随机选取单词
    this.random();
    // 获取单词的详细信息
    this.intilise();

  },
  // 初始化复习数组
  intilise: function () {
    for (var i = 0; i < this.data.goal; i++) {
      this.getaudiourl()
    }
    console.log("复习的数组", this.data.List);
  },

  // 获取随机数
  random: function () {
    // 生成取随机数的数组
    var arr = new Array(this.data.Id)
    var i = arr.length
    while (i--) {
      arr[i] = i
    }
    this.setData({
      array: arr
    })
    //console.log(arr)
    // 交换法获取不重复的随机数的数组
    var result = [];
    var ranNum = this.data.goal;
    for (var i = 0; i < ranNum; i++) {
      var ran = Math.floor(Math.random() * (arr.length - i));
      result.push(arr[ran]);
      arr[ran] = arr[arr.length - i - 1];
    };
    this.setData({
      result: result
    })
  },
  // 获取单词的相关信息
  getaudiourl: function () {
    // 随机选取已学的单词听写
    // this.random()
    var idx = this.data.result[this.data.count]
    let that = this;
    var count = this.data.count
    var record = 'List[' + count + ']'
    //console.log("随机值", idx)
    //console.log("计数器：",this.data.count)
    wx.request({
      url: 'https://api.shanbay.com/bdc/search/?word=' + this.data.review_word[idx],
      data: {},
      method: 'GET',
      success: function (res) {
        console.log(res)
        that.setData({
          content: res.data.data.content,
          audioURL: res.data.data.audio,
          pron: '/' + res.data.data.pronunciation + '/',
          definition: res.data.data.definition,
          [record]: {
            word: res.data.data.content,
            pron: '/' + res.data.data.pronunciation + '/',
            definition: res.data.data.definition,
            audioURL: res.data.data.audio,
            showNot_definition: true,
            showNot_word: false,
            id: count * 1 - 1,
          }
        })
      }
    })
    this.setData({
      idx: idx
    })
    // 计数器加一
    this.setData({
      count: ++count
    })
  },
  // 单词发声
  read(e) {
    var audiourl = e.currentTarget.id;
    const innerAudioContext = wx.createInnerAudioContext()
    innerAudioContext.autoplay = true
    innerAudioContext.src = audiourl
    innerAudioContext.play()
    innerAudioContext.onPlay(() => { })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
  },
  // 翻译显示
  show: function (e) {
    var id = e.currentTarget.id;
    console.log("选择的id值", id);

    if (this.data.show_definition == false && this.data.showNot_word == false) {
      // 翻译显示控制
      var change = 'List[' + id + '].showNot_definition'
      this.setData({
        [change]: false
      })
      //单词显示控制器 
      var b = !this.data.List[id].showNot_definition
      console.log("showNot_word:", b)
      var change = 'List[' + id + '].showNot_word'
      this.setData({
        [change]: false
      })
    } else {
      var a = !this.data.List[id].showNot_definition
      console.log("showNot_definition:", a)
      var change = 'List[' + id + '].showNot_definition'
      this.setData({
        [change]: a
      })
    }


  },

  // 中英文显示切换
  switch1Change: function () {
    // 单词显示翻译隐藏的话
    if (this.data.show_definition == true && this.data.show_word == false) {
      // console.log("走了1")
      // 翻译显示单词隐藏
      var change_definition
      var change_word
      for (var i in this.data.List) {
        change_word = 'List[' + i + '].showNot_word'
        change_definition = 'List[' + i + '].showNot_definition'
        this.setData({
          [change_word]: true,
          [change_definition]: false,
          show_word: true,
          show_definition: false,
        })
      }
    } else if (this.data.show_definition == false && this.data.show_word == false) {
      var change_definition
      var change_word
      for (var i in this.data.List) {
        change_word = 'List[' + i + '].showNot_word'
        change_definition = 'List[' + i + '].showNot_definition'
        this.setData({
          [change_word]: false,
          [change_definition]: false,
          show_word: false,
          show_definition: false,
        })
      }
    } else {
      // 单词显示翻译隐藏
      var change_definition
      var change_word
      for (var i in this.data.List) {
        change_word = 'List[' + i + '].showNot_word'
        change_definition = 'List[' + i + '].showNot_definition'
        this.setData({
          [change_word]: false,
          [change_definition]: true,
          show_word: false,
          show_definition: true,
        })
      }
    }

  },
  // 同时显示
  switch2Change: function () {
    var change_definition
    var change_word
    if (this.data.show_definition == true && this.data.show_word == false) {
      for (var i in this.data.List) {
        change_word = 'List[' + i + '].showNot_word'
        change_definition = 'List[' + i + '].showNot_definition'
        this.setData({
          [change_word]: false,
          [change_definition]: false,
          show_word: false,
          show_definition: false,
        })
      }
    } else if (this.data.show_definition == false && this.data.show_word == true) {
      for (var i in this.data.List) {
        change_word = 'List[' + i + '].showNot_word'
        change_definition = 'List[' + i + '].showNot_definition'
        this.setData({
          [change_word]: false,
          [change_definition]: false,
          show_word: false,
          show_definition: false,
        })
      }
    } else {
      for (var i in this.data.List) {
        change_word = 'List[' + i + '].showNot_word'
        change_definition = 'List[' + i + '].showNot_definition'
        this.setData({
          [change_word]: false,
          [change_definition]: true,
          show_word: false,
          show_definition: true,
        })
      }
    }


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})